package p.ithorns.example.statemachine;


import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * InMemoryStateMachinePersist
 *
 * @author Ht.L
 * @date 2023-09-09 17:12
 * @since 1.0.0
 */
public class InMemoryStateMachinePersist implements StateMachinePersist<States, Events, StateObj> {

    private final static Map<String, StateMachineContext<States, Events>> cache = new ConcurrentHashMap<>();

    @Override
    public void write(StateMachineContext<States, Events> context, StateObj obj) throws Exception {
        cache.put(obj.getId(), context);
    }

    @Override
    public StateMachineContext<States, Events> read(StateObj contextObj) throws Exception {
        return cache.get(contextObj.getId());
    }
}